package com.jinlia.show.common.core.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;


public class PageUtils {


    public static <T> Page<T> tToVO(Page page, Class<T> tClass) {
        Page<T> t2Page = new Page<T>();
        t2Page.setTotal(page.getTotal());
        if (page.getTotal() == 0) {
            return t2Page;
        }
        t2Page.setSize(page.getSize());
        t2Page.setCurrent(page.getCurrent());
        List records = page.getRecords();
        t2Page.setRecords(new ArrayList<T>());
        for (Object record : records) {
            try {
                T t = tClass.getConstructor().newInstance();
                BeanUtils.copyProperties(record, t);
                t2Page.getRecords().add(t);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        return t2Page;
    }
}
